package city.spring.modules.medicine.service.impl;

import city.spring.modules.medicine.dto.MedicinestorehouseDTO;
import city.spring.modules.medicine.entity.MedicineshelfEntity;
import city.spring.modules.medicine.entity.MedicineshelflevelEntity;
import city.spring.modules.medicine.entity.MedicinestorehouseEntity;
import city.spring.modules.medicine.repository.MedicinestorehouseRepository;
import city.spring.modules.medicine.service.MedicineshelfService;
import city.spring.modules.medicine.service.MedicineshelflevelService;
import city.spring.modules.medicine.service.MedicinestorehouseService;
import city.spring.modules.medicine.vo.MedicinestorehouseVO;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.data.domain.Pageable;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@CacheConfig(cacheNames = {MedicinestorehouseServiceImpl.CACHE_NAME})
@Transactional(rollbackFor = Throwable.class)
@Service
public class MedicinestorehouseServiceImpl extends ServiceImpl<MedicinestorehouseRepository, MedicinestorehouseEntity> implements MedicinestorehouseService {

    private final MedicinestorehouseRepository storehouseRepository;
    private final MedicineshelfService shelfService;
    private final MedicineshelflevelService shelflevelService;

    public MedicinestorehouseServiceImpl(MedicinestorehouseRepository storehouseRepository, MedicineshelfService shelfService, MedicineshelflevelService shelflevelService) {
        this.storehouseRepository = storehouseRepository;
        this.shelfService = shelfService;
        this.shelflevelService = shelflevelService;
    }

    @Async
    @Override
    public void saveStorehouse(MedicinestorehouseDTO entity) {
        if (!save(entity)) {
            throw new RuntimeException("保存信息失败");
        }

        Integer shelfs = entity.getShelfs();
        Integer shelflevels = entity.getShelflevels();
//        Integer beds = entity.getBeds();
        if (shelfs == null || shelfs < 0) {
            return;
        }
        if (shelflevels == null || shelflevels < 0) {
            return;
        }

        // 先保存所有货架信息
        List<MedicineshelfEntity> shelfEntities = new ArrayList<>();
        for (int shelfIndex = 0; shelfIndex < shelfs; shelfIndex++) {
            MedicineshelfEntity shelfEntity = new MedicineshelfEntity();
            shelfEntity.setId(IdWorker.getIdStr());
            shelfEntity.setStorehouseId(String.valueOf(entity.getId()));
            shelfEntity.setCode(String.valueOf(shelfIndex + 1));
            shelfEntity.setSname(String.format("%d号", shelfIndex + 1));
            shelfEntity.setType(entity.getType());
            shelfEntities.add(shelfEntity);
        }

        // 然后保存所有楼层的房间信息
//        List<BedRoomEntity> roomEntities = new ArrayList<>();
//        for (int floorIndex = 0; floorIndex < floorEntities.size(); floorIndex++) {
//            BedFloorEntity floorEntity = floorEntities.get(floorIndex);
//
//            for (int roomIndex = 0; roomIndex < rooms; roomIndex++) {
//                BedRoomEntity roomEntity = new BedRoomEntity();
//                roomEntity.setId(IdWorker.getIdStr());
//                roomEntity.setBedFloorId(floorEntity.getId());
//                roomEntity.setRoomTypeId(entity.getRoomsType());
//                roomEntity.setCode(String.format("%d%02d", floorIndex + 1, roomIndex + 1));
//                roomEntity.setName(String.format("%d%02d房间", floorIndex + 1, roomIndex + 1));
//                roomEntity.setDirection(entity.getRoomsDirection());
//                roomEntities.add(roomEntity);
//            }
//        }

        // 最后保存所有货架的层次信息
        List<MedicineshelflevelEntity> shelflevelEntities = new ArrayList<>();
        for (MedicineshelfEntity shelfEntity : shelfEntities) {
            for (int shelflevelIndex = 0; shelflevelIndex < shelflevels; shelflevelIndex++) {
                MedicineshelflevelEntity shelfLevelEntity = new MedicineshelflevelEntity();
                shelfLevelEntity.setId(IdWorker.getIdStr());
                shelfLevelEntity.setShelfId(shelfEntity.getId());
                shelfLevelEntity.setCode(String.valueOf(shelflevelIndex + 1));
                shelfLevelEntity.setSname(String.format("%d层", shelflevelIndex + 1));
                shelflevelEntities.add(shelfLevelEntity);
            }
        }
        shelfService.saveBatch(shelfEntities);
        shelflevelService.saveBatch(shelflevelEntities);
    }

    @Override
    public void updateStorehouse(MedicinestorehouseEntity entity) {
        // 使用 updateById(entity); 修改数据时，将会修改 entity 对象中所有非null数据，如果某个字段为null，将会忽略该字段的修改
        boolean update = updateById(entity);
        if (!update) {
            throw new RuntimeException("修改信息失败");
        }
    }

    @Override
    public void deleteStorehouse(String primaryKey) {
        removeById(primaryKey);
    }

    @Override
    public void deleteStorehouse(List<String> primaryKeys) {
        removeByIds(primaryKeys);
    }

    /**
     * 获取仓库管理员名称
     *
     * @return
     */
    @Override
    public List<MedicinestorehouseEntity> getStorehouseAllAndName() {
        return storehouseRepository.getStorehouseAllAndName();
    }

    @Override
    public MedicinestorehouseEntity getById(String id) {
        return storehouseRepository.getById(id);
    }

    @Override
    public List<MedicinestorehouseEntity> getStorehouseByStypeIsMedicine() {
        return storehouseRepository.getStorehouseByStypeIsMedicine();
    }


    @Override
    public IPage<MedicinestorehouseEntity> getStorehouseStypeIsMedicine(Pageable pageable) {
        IPage<MedicinestorehouseEntity> page = MyBatisUtils.toPage(pageable);
        return storehouseRepository.getStorehouseStypeIsMedicine(page, lambdaQuery().getWrapper());
    }

    @Override
    public IPage<MedicinestorehouseVO> listPage(Page<? extends MedicinestorehouseEntity> page, Wrapper<? extends MedicinestorehouseEntity> wrapper) {
        return baseMapper.listPage(page, wrapper);
    }

}
